package news

import (
	"parking-lot-go-dingding/config"
	"parking-lot-go-dingding/controller"
	"parking-lot-go-dingding/model/dingtalk"

	"github.com/gin-gonic/gin"
)

func Detail(c *gin.Context) {

	requestStruct := struct {
		Id int `json:"id"`
	}{}

	//接收加密串
	if err := c.ShouldBindJSON(&requestStruct); err != nil {
		controller.Fail(c, "json接收失败")
		return
	}

	var dingtalkNewsStruct dingtalk.DingtalkNewsStruct
	dingtalkNewsData, err := dingtalkNewsStruct.First(requestStruct.Id)
	if err != nil {
		controller.Fail(c, err.Error())
		return
	}

	dbSection := config.Cfg.Section("SYSTEM")
	imageUrl := dbSection.Key("IMAGE_URL").String()

	returnData := map[string]interface{}{
		"title":      dingtalkNewsData.Title,
		"introduce":  dingtalkNewsData.Introduce,
		"content":    dingtalkNewsData.Content,
		"image_path": imageUrl + dingtalkNewsData.ImagePath,
		"sort":       dingtalkNewsData.Sort,
		"updated_at": dingtalkNewsData.UpdatedAt.Format("2006-01-02 15:04"),
	}

	controller.Success(c, returnData)
}
